﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Diagnostics;

namespace Indt.Components
{
    public class IWebClient
    {
        private WebClient _webClient;
        public string ContentType { get; set; }
        public string GetEncoding { get; set; }
        public bool Credentials { get; set; }
        public string ServerMode { get; set; }
        public string ServerHost { get; set; }
        public string ServerPort { get; set; }

        public IWebClient()
        {
            _webClient = new WebClient();
            ContentType = "text/html; charset=ISO-8859-1";
            GetEncoding = "ISO-8859-1";
            Credentials = true;
            ServerPort = null;
            
        }

        public void Post(string service, string data, Action<string> success, Action<HttpWebResponse> fail)
        {
            _webClient.Headers["Content-type"] = ContentType;
            _webClient.UseDefaultCredentials = Credentials;

            string uri = ServerMode + ServerHost;

            if (ServerPort != null && ServerPort != "")
            {
                uri += ":" + ServerPort;
            }

            if (service != null && service != "")
            {
                uri += service;
            }

            Uri _uri = new Uri(uri);

            Debug.WriteLine("service: " + _uri + "\ndata: " + data);

            UploadStringCompletedEventHandler handler = null;

            handler = (s, e) =>
            {
                _webClient.UploadStringCompleted -= handler;

                if (!e.Cancelled)
                {
                    if (e.Error == null)
                    {
                        string result = e.Result;
                        success(result);
                    }
                    else
                    {
                        fail((HttpWebResponse)((WebException)e.Error).Response);
                    }
                }
            };

            _webClient.UploadStringCompleted += handler;
            _webClient.UploadStringAsync(_uri, data);
        }

        public void Get(string service, Action<string> success, Action<HttpWebResponse> fail)
        {
            _webClient.Encoding = Encoding.GetEncoding(GetEncoding);
            _webClient.UseDefaultCredentials = Credentials;
            DownloadStringCompletedEventHandler handler = null;

            string uri = ServerMode + ServerHost;

            if (ServerPort != null && ServerPort != "")
            {
                uri += ":" + ServerPort;
            }

            if (service != null && service != "")
            {
                uri += service;
            }

            Uri _uri = new Uri(uri);

            Debug.WriteLine("service: " + _uri);

            handler = (s, e) =>
            {
                _webClient.DownloadStringCompleted -= handler;

                if (!e.Cancelled)
                {
                    if (e.Error == null)
                    {
                        string result = e.Result;
                        success(result);
                    }
                    else
                    {
                        fail((HttpWebResponse)((WebException)e.Error).Response);
                    }
                }
            };

            _webClient.DownloadStringCompleted += handler;
            _webClient.DownloadStringAsync(_uri);
        }
    }
}
